﻿using GenerateProtocol.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;

namespace GenerateProtocol.Helper
{
    public static class Extension
    {
        /// <summary>
        /// 添加实体属性
        /// </summary>
        /// <param name="AttributeItemTypeList"></param>
        /// <param name="itemCode"></param>
        /// <param name="itemValue"></param>
        /// <param name="itemName"></param>
        /// <param name="existUp">如果存在该key是否更新</param>
        public static void AddAttributeItemType(List<AttributeItemType> AttributeItemTypeList, string itemCode, object itemValue, string itemName, bool existUp = false)
        {
            try
            {
                AttributeItemType entityItem = null;
                //检查属性是否已经添加
                if (IsExistAttribute(AttributeItemTypeList, itemCode))
                {
                    if (existUp)
                    {
                        entityItem = AttributeItemTypeList.FindLast(x => x.ItemCode.Equals(itemCode, StringComparison.OrdinalIgnoreCase));
                    }
                    else
                    {
                        return;
                    }
                }

                if (entityItem == null)
                {
                    entityItem = new AttributeItemType();
                    AttributeItemTypeList.Add(entityItem);
                }
                entityItem.ItemCode = itemCode;

                if (!string.IsNullOrEmpty(itemName))
                {
                    entityItem.ItemName = itemName;
                }

                if (itemValue is string)
                {
                    entityItem.Value = (string)itemValue;
                }
                else if (itemValue is long)
                {
                    entityItem.Value = itemValue.ToString();
                }
                else if (itemValue is decimal)
                {
                    entityItem.Value = itemValue.ToString();
                }
                else if (itemValue is double)
                {
                    entityItem.Value = itemValue.ToString();
                }
                else if (itemValue is float)
                {
                    entityItem.Value = itemValue.ToString();
                }
            }
            catch (Exception E01)
            {
                Console.WriteLine(E01.ToString(), "添加实体属性失败, itemCode" + itemCode);
            }
        }

        /// <summary>
        /// 检查属性是否存在
        /// </summary>
        /// <param name="list_EntityData"></param>
        /// <param name="itemCode"></param>
        /// <returns></returns>
        private static bool IsExistAttribute(List<AttributeItemType> list_EntityData, string itemCode)
        {
            if (list_EntityData != null && list_EntityData.Count > 0)
            {
                foreach (var item in list_EntityData)
                {
                    if (item.ItemCode.Equals(itemCode, StringComparison.OrdinalIgnoreCase))
                    {
                        return true;
                    }
                }
            }
            return false;
        }


        public static string GetMd5Hash(string input)
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);
                byte[] hashBytes = md5.ComputeHash(inputBytes);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }

        public static readonly string[] VideoExtensions =
        {
            ".avi", ".mp4", ".m2ts", ".mxf", ".mts", ".mov", ".3gp", ".divx", ".mpg", ".mpeg",
            ".ts", ".dat", ".vob", ".rmvb", ".rm", ".mkv", ".wmv" , ".m3u8"
        };

        public static readonly string[] PictureExtensions =
        {
            ".png", ".jpg", ".gif", ".bmp", ".dib", ".jpeg", ".jpe", ".jfif", ".tif", ".tiff", ".tga"
        };

        public static readonly string[] AudioExtensions =
        {
            ".wav", ".mp3", ".wma", ".ac3", ".aac", ".dts", ".mid", ".midi", ".ogg",
            ".ape", ".flac"
        };

        public static readonly string[] DocumentExtensions =
        {
            ".txt", ".xps", ".xls", ".xlsx", ".csv", ".ppt", ".pptx", ".doc", ".docx", ".rtf", ".pdf"
        };

        public static string GetEntityType(string path)
        {
            var ext = GetFileExtension(path, true);
            if (!string.IsNullOrEmpty(ext))
                ext = ext.ToLower();
            if (VideoExtensions.Contains(ext))
                return ClipTypeHive.biz_sobey_video.ToString();
            if (PictureExtensions.Contains(ext))
                return ClipTypeHive.biz_sobey_picture.ToString();
            if (AudioExtensions.Contains(ext))
                return ClipTypeHive.biz_sobey_audio.ToString();
            if (DocumentExtensions.Contains(ext))
                return ClipTypeHive.biz_sobey_document.ToString();
            return ClipTypeHive.biz_sobey_other.ToString();
        }

        /// <summary>
        /// 获取文件扩展名(支持格式:windows、unc、ftp、oss、http)
        /// </summary>
        /// <param name="filePath">文件名称(必须包含后缀名称)</param>
        /// <param name="isOnlyExt">是否只获取扩展名称(即:如果是oss路径是否去掉扩展名后面的内容 ?product=aliyun&type=http&version=2 )(true 是,false 不是)</param>
        /// <returns></returns>
        public static string GetFileExtension(string filePath, bool isOnlyExt)
        {
            try
            {
                string ext = Path.GetExtension(filePath);//此处oss取出的格式是 .mp4?product=aliyun&type=http&version=2
                if (isOnlyExt)
                {
                    if (filePath.IndexOf("oss:") > -1 && ext.IndexOf("?") > -1)
                    {
                        //oss路径: oss://LCUoZfq8fhOiQfV5:Vu2fPUkDwi2x0e3EoQmoa2SYeYlYfw@oss-cn-beijing-xhcloud-d01-a.xhcloud.news.cn/beisuo-neirongku/u-2lp90gefz05b34zl/190513/17120779/11test15帧素材.mp4?product=aliyun&type=http&version=2
                        ext = ext.Substring(0, ext.IndexOf("?"));//只取文件扩展名称
                    }
                }
                return ext;
            }
            catch (Exception e01)
            {
                Console.WriteLine(e01.ToString(), $"获取文件后缀失败!filePath:{filePath}");
            }
            return string.Empty;
        }
    }
}
